home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / Onboard / KeyboardGTK.py < prev    next >
Text File  |  2009-10-01  |  5KB  |  153 lines

  1. ### Logging ###
  2. import logging
  3. _logger = logging.getLogger("KeyboardGTK")
  4. ###############
  5.  
  6. import gtk
  7. import gobject
  8. import pango
  9.  
  10. ### Config Singleton ###
  11. from Onboard.Config import Config
  12. config = Config()
  13. ########################
  14.  
  15. class KeyboardGTK(gtk.DrawingArea):
  16.  
  17.     scanning_time_id = None
  18.  
  19.     def __init__(self):
  20.         gtk.DrawingArea.__init__(self)
  21.         self.add_events(gtk.gdk.BUTTON_PRESS_MASK 
  22.                       | gtk.gdk.BUTTON_RELEASE_MASK 
  23.                       | gtk.gdk.LEAVE_NOTIFY_MASK)
  24.  
  25.         self.connect("expose_event",         self.expose)
  26.         self.connect("button_press_event",   self._cb_mouse_button_press)
  27.         self.connect("button_release_event", self._cb_mouse_button_release)
  28.         self.connect("leave-notify-event",   self._cb_mouse_leave)
  29.         self.connect("configure-event",      self._cb_configure_event)
  30.         config.scanning_notify_add(self.reset_scan)
  31.  
  32.     def _cb_configure_event(self, widget, user_data):
  33.         size = self.get_allocation()
  34.         self.kbwidth = size.width - config.SIDEBARWIDTH # to allow for sidebar
  35.         self.height = size.height
  36.  
  37.         # For key label size calculations
  38.         pango_context = self.create_pango_context()
  39.         for pane in [self.basePane,] + self.panes:
  40.             pane.on_size_changed(self.kbwidth, self.height, pango_context)
  41.             pane.configure_labels(self.mods, pango_context)
  42.                     
  43.     def _cb_mouse_leave(self, widget, grabbed):
  44.         """ 
  45.         horrible.  Grabs pointer when key is pressed, released when cursor 
  46.         leaves keyboard
  47.         """
  48.  
  49.         gtk.gdk.pointer_ungrab() 
  50.         if self.active:
  51.             if self.scanningActive:
  52.                 self.active = None      
  53.                 self.scanningActive = None
  54.             else:       
  55.                 self.release_key(self.active)
  56.             self.queue_draw()
  57.         return True
  58.  
  59.     def _cb_mouse_button_release(self,widget,event):
  60.         if self.active:
  61.             #self.active.on = False
  62.             self.release_key(self.active)
  63.             if len(self.stuck) > 0:
  64.                 for stick in self.stuck:
  65.                     self.release_key(stick)
  66.                 self.stuck = []
  67.             self.active = None
  68.  
  69.         self.queue_draw()
  70.         return True
  71.  
  72.     def _cb_mouse_button_press(self,widget,event):
  73.         gtk.gdk.pointer_grab(self.window, True)
  74.         if event.type == gtk.gdk.BUTTON_PRESS:
  75.             self.active = None#is this doing anything
  76.             
  77.             if config.scanning and self.basePane.columns:
  78.                 if self.scanning_time_id:
  79.                     if not self.scanning_y == None:
  80.                         self.press_key(self.scanningActive)
  81.                         gobject.source_remove(self.scanning_time_id)
  82.                         self.reset_scan()
  83.                     else:
  84.                         self.scanning_y = -1
  85.                         gobject.source_remove(self.scanning_time_id)
  86.                         self.scanning_time_id = gobject.timeout_add(
  87.                                 config.scanning_interval, self.scan_tick)
  88.                 else:   
  89.                     self.scanning_time_id = gobject.timeout_add(
  90.                         config.scanning_interval, self.scan_tick)
  91.                     self.scanning_x = -1
  92.             else:
  93.                 #TODO tabkeys should work like the others
  94.                 for key in self.tabKeys:
  95.                     self.is_key_pressed(key, widget, event)
  96.                 context = self.window.cairo_create()
  97.                 if self.activePane:
  98.                     key = self.activePane.get_key_at_location(
  99.                         (event.x, event.y), context)
  100.                 else:
  101.                     key = self.basePane.get_key_at_location(
  102.                         (event.x, event.y), context)
  103.                 if key: self.press_key(key)
  104.         return True 
  105.         
  106.     #Between scans and when value of scanning changes.
  107.     def reset_scan(self, scanning=None):
  108.         if self.scanningActive:
  109.             self.scanningActive.beingScanned = False
  110.         if self.scanning_time_id:
  111.             gobject.source_remove(self.scanning_time_id)
  112.             self.scanning_time_id = None
  113.  
  114.         self.scanning_x = None
  115.         self.scanning_y = None
  116.         self.queue_draw()
  117.  
  118.     def expose(self, widget, event):
  119.         context = widget.window.cairo_create()
  120.         context.set_line_width(1.1)
  121.  
  122.         context.set_source_rgba(float(self.basePane.rgba[0]),
  123.                     float(self.basePane.rgba[1]),
  124.                     float(self.basePane.rgba[2]),
  125.                     float(self.basePane.rgba[3]))#get from .sok
  126.         context.paint()
  127.  
  128.  
  129.         self.basePane.paint(context)
  130.  
  131.         if (self.activePane):
  132.  
  133.             context.rectangle(0, 0, self.kbwidth, self.height)
  134.             context.set_source_rgba(float(self.activePane.rgba[0]),
  135.                         float(self.activePane.rgba[1]),
  136.                         float(self.activePane.rgba[2]),
  137.                         float(self.activePane.rgba[3]))#get from .sok
  138.             context.fill()
  139.             self.activePane.paint(context)
  140.  
  141.         for key in self.tabKeys:
  142.             key.paint(context)
  143.  
  144.         return True
  145.  
  146.     def _on_mods_changed(self):
  147.         _logger.info("Modifiers have been changed")
  148.         context = self.create_pango_context()
  149.         for pane in [self.basePane,] + self.panes:
  150.             pane.configure_labels(self.mods, context)
  151.  
  152.  
  153.